<?php
class Admin_LibphotoController extends Zend_Controller_Action {
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != "login") {
				$this->_redirect ( "/admin/index/login" );
			}
		}
	}
	public function init() {
		/* Initialize action controller here */
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$this->view->fullName = $infoUser->USERNAME;
		$this->_model = new Admin_Model_DbTable_LibraryPhoto ();
		Zend_Layout::startMvc ( array (
				"layoutPath" => APPLICATION_PATH . "/layouts/scripts",
				"layout" => "admin" 
		) );
	}
	public function indexAction() {
		$status = $this->getRequest ()->getParam ( "sorting" );
		// action body
		$this->view->headTitle ( "Quản lý thư viện hình ảnh", "PREPEND" );
		$this->view->title = "Quản lý thư viện hình ảnh";
		if ($status == null)
			$lib = $this->_model->getAllLibPhoto ();
		else
			$lib = $this->_model->getLibPhotoByStatus ( $status );
		$paginator = Zend_Paginator::factory ( $lib );
		$paginator->setItemCountPerPage ( 9 );
		$paginator->setPageRange ( 5 );
		$currentPage = $this->_request->getParam ( "page", 1 );
		$paginator->setCurrentPageNumber ( $currentPage );
		$this->view->allLib = $paginator;
	}
	public function addAction() {
		$this->view->headTitle ( "Thêm thư viện hình ảnh", "PREPEND" );
		$this->view->title = "Thêm thư viện hình ảnh";
		$upload = new Private_Upload ();
		if ($this->getRequest ()->isPost ()) {
			$Name = $this->_getParam ( "LibName" );
			$DOn = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOn" ) ) );
			$DOff = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOff" ) ) );
			$Desc = $this->_getParam ( "Desc" );
			$Stt = $this->_getParam ( "stt" );
			if ($_FILES ['PImg'] ['name'] == null && $this->_request->getParam ( "textImg" ) == "") {
				$this->view->errImg = "BÃ¡ÂºÂ¡n chÃ†Â°a chÃ¡Â»ï¿½n hÃƒÂ¬nh Ã¡ÂºÂ£nh";
			} elseif ($this->_request->getParam ( "textImg" ) != null && $_FILES ['PImg'] ['name'] == null) {
				$img = $this->_request->getParam ( "textImg" );
				$this->_model->addLibPhoto ( $Name, $DOn, $DOff, $Desc, $img, $Stt );
				$this->_redirect ( "admin/libphoto" );
			} else {
				$img = "/ishali/public/uploaded/images/photo/" . $_FILES ['PImg'] ["name"];
				$upload->upload ( "photo" );
				$this->_model->addLibPhoto ( $Name, $DOn, $DOff, $Desc, $img, $Stt );
				$this->_redirect ( "admin/libphoto" );
			}
		}
	}
	public function editAction() {
		$this->view->headTitle ( "Chỉnh sửa thư viện hình ảnh", "PREPEND" );
		$this->view->title = "Chỉnh sửa thư viện hình ảnh";
		$id = $this->_getParam ( "id" );
		$this->view->lib = $this->_model->getLibPhotoById ( $id, 1 );
		if ($this->getRequest ()->isPost ()) {
			$Name = $this->_getParam ( "LibName" );
			$DOn = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOn" ) ) );
			$DOff = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOff" ) ) );
			$Desc = $this->_getParam ( "Desc" );
			$Stt = $this->_getParam ( "stt" );
			$upload = new Private_Upload ();
			if ($this->getRequest ()->isPost ()) {
				$Name = $this->_getParam ( "LibName" );
				$DOn = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOn" ) ) );
				$DOff = date ( "Y-m-d", strtotime ( $this->_getParam ( "DateOff" ) ) );
				$Desc = $this->_getParam ( "Desc" );
				$Stt = $this->_getParam ( "stt" );
				if ($Stt == 0) {
					$photo = new Default_Model_DbTable_Photo ();
					$list = $photo->GetByLibId ( $id );
					foreach ( $list as $l ) {
						$photo->updateStatus ( $l ["ID"], 0 );
					}
					if ($_FILES ['PImg'] ['name'] == null && $this->_request->getParam ( "textImg" ) == "") {
						$this->view->errImg = "Vui lòng chọn hình ảnh!";
					} elseif ($this->_request->getParam ( "textImg" ) != null && $_FILES ['PImg'] ['name'] == null) {
						$img = $this->_request->getParam ( "textImg" );
						$this->_model->updateLibPhoto ( $id, $Name, $DOn, $DOff, $Desc, $img, $Stt );
						$this->_redirect ( "admin/libphoto" );
					} else {
						$img = "/ishali/public/uploaded/images/photo/" . $_FILES ['PImg'] ["name"];
						$upload->upload ( "photo" );
						$this->_model->updateLibPhoto ( $id, $Name, $DOn, $DOff, $Desc, $img, $Stt );
						$this->_redirect ( "admin/libphoto" );
					}
				} else {
					$photo = new Default_Model_DbTable_Photo ();
					$list = $photo->GetByLibId ( $id );
					foreach ( $list as $l ) {
						$photo->updateStatus ( $l ["ID"], 1 );
					}
					if ($_FILES ['PImg'] ['name'] == null && $this->_request->getParam ( "textImg" ) == "") {
						$this->view->errImg = "BÃ¡ÂºÂ¡n chÃ†Â°a chÃ¡Â»ï¿½n hÃƒÂ¬nh Ã¡ÂºÂ£nh";
					} elseif ($this->_request->getParam ( "textImg" ) != null && $_FILES ['PImg'] ['name'] == null) {
						$img = $this->_request->getParam ( "textImg" );
						$this->_model->updateLibPhoto ( $id, $Name, $DOn, $DOff, $Desc, $img, $Stt );
						$this->_redirect ( "admin/libphoto" );
					} else {
						$img = "/ishali/public/uploaded/images/photo/" . $_FILES ['PImg'] ["name"];
						$upload->upload ( "photo" );
						$this->_model->updateLibPhoto ( $id, $Name, $DOn, $DOff, $Desc, $img, $Stt );
						$this->_redirect ( "admin/libphoto" );
					}
				}
			}
		}
	}
	public function delAction() {
		$id = $this->_getParam ( "id" );
		$photo = new Default_Model_DbTable_Photo ();
		$list = $photo->GetByLibId ( $id );
		foreach ( $list as $l ) {
			$photo->DelPhoto ( $l ["ID"] );
		}
		$this->_model->removeLibPhoto ( $id );
		$this->_redirect ( "admin/libphoto" );
	}
	public function activeAction() {
		$id = $this->_getParam ( "id" );
		$this->_model->active ( $id );
		$photo = new Default_Model_DbTable_Photo ();
		$lib = $this->_model->getLibPhotoById ( $id, 1 );
		$list = $photo->GetByLibId ( $id );
		foreach ( $list as $l ) {
			$photo->updateStatus ( $l ["ID"], $lib ["STATUS"] );
		}
		$this->_redirect ( "admin/libphoto" );
	}
}

